package com.cykj.pos.controller.api;

import java.util.List;
import java.util.Arrays;

import com.cykj.common.core.domain.entity.SysUser;
import com.cykj.common.utils.SecurityUtils;
import com.cykj.pos.domain.BizMerchantReceiver;
import com.cykj.pos.domain.dto.BizMerchantReceiverDTO;
import com.cykj.pos.service.IBizMerchantReceiverService;
import com.cykj.pos.service.IBizMerchantService;
import com.cykj.system.service.ISysUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.cykj.common.annotation.Log;
import com.cykj.common.core.controller.BaseController;
import com.cykj.common.core.domain.AjaxResult;
import com.cykj.common.enums.BusinessType;
import com.cykj.pos.domain.BizWithdrawalRecords;
import com.cykj.pos.service.IBizWithdrawalRecordsService;
import com.cykj.common.utils.poi.ExcelUtil;
import com.cykj.common.core.page.TableDataInfo;

/**
 * 提现记录Controller
 *
 * @author weijianbo
 * @date 2025-02-12
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/pos/withdrawal/records" )
public class BizWithdrawalRecordsController extends BaseController {

    private final IBizWithdrawalRecordsService iBizWithdrawalRecordsService;

    private final IBizMerchantReceiverService iBizMerchantReceiverService;

    private final IBizMerchantService bizMerchantService;

    private final ISysUserService iSysUserService;
    /**
     * 查询提现记录列表
     */
//    @PreAuthorize("@ss.hasPermi('pos:withdrawal:list')")
    @GetMapping("/list")
    public TableDataInfo list(BizWithdrawalRecords bizWithdrawalRecords) {
        if(bizWithdrawalRecords.getDeptId() !=null && !"".equals(bizWithdrawalRecords.getDeptId())){
            List<Long> userIds = iSysUserService.queryUserByDeptId(bizWithdrawalRecords.getDeptId());
            bizWithdrawalRecords.setUserIds(userIds);
        }
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        if(sysUser.getUserId() == 2L || sysUser.getUserId() == 1L){
            bizWithdrawalRecords.setRemark("安心付-闯业");
        }else if(sysUser.getUserId() == 23L){
            bizWithdrawalRecords.setRemark("安心付-伙伴");
        }else if(sysUser.getUserId() == 39L){
            bizWithdrawalRecords.setRemark("安心付-乐富");
        }
        startPage();
        List<BizWithdrawalRecords> list = iBizWithdrawalRecordsService.queryList(bizWithdrawalRecords);
        for(BizWithdrawalRecords records : list){
            if("0".equals(records.getPayStatus())){
                String merchId = bizMerchantService.selectMerchId(records.getUserId().toString());
                BizMerchantReceiverDTO bizMerchantReceiverDTO = new BizMerchantReceiverDTO();
                bizMerchantReceiverDTO.setMerchId(Long.parseLong(merchId));
                bizMerchantReceiverDTO.setUserId(records.getUserId());
                BizMerchantReceiver bizMerchantReceiver1 = iBizMerchantReceiverService.bizMerchantReceiver(bizMerchantReceiverDTO);
                if (bizMerchantReceiver1 != null && bizMerchantReceiver1.getContractStatus().equals("2")) {
                    records.setContractStatus("2");
                }else{
                    records.setContractStatus("0");
                }
            }
            if("0".equals(records.getPayStatus())){
                records.setPayStatus("未打款");
            }else if ("1".equals(records.getPayStatus())){
                records.setPayStatus("已打款");
            }else if ("2".equals(records.getPayStatus())){
                records.setPayStatus("已驳回");
            }else{
                records.setPayStatus("进行中");
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出提现记录列表
     */
//    @PreAuthorize("@ss.hasPermi('pos:withdrawal:export')" )
    @Log(title = "提现记录" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(BizWithdrawalRecords bizWithdrawalRecords) {
        List<BizWithdrawalRecords> list = iBizWithdrawalRecordsService.queryList(bizWithdrawalRecords);
        ExcelUtil<BizWithdrawalRecords> util = new ExcelUtil<BizWithdrawalRecords>(BizWithdrawalRecords.class);
        return util.exportExcel(list, "records" );
    }

    /**
     * 获取提现记录详细信息
     */
//    @PreAuthorize("@ss.hasPermi('pos:withdrawal:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iBizWithdrawalRecordsService.getById(id));
    }

    /**
     * 新增提现记录
     */
//    @PreAuthorize("@ss.hasPermi('pos:withdrawal:add')" )
    @Log(title = "提现记录" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BizWithdrawalRecords bizWithdrawalRecords) {
        return toAjax(iBizWithdrawalRecordsService.save(bizWithdrawalRecords) ? 1 : 0);
    }

    /**
     * 修改提现记录
     */
//    @PreAuthorize("@ss.hasPermi('pos:withdrawal:edit')" )
    @Log(title = "提现记录" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizWithdrawalRecords bizWithdrawalRecords) {
        return toAjax(iBizWithdrawalRecordsService.updateById(bizWithdrawalRecords) ? 1 : 0);
    }

    /**
     * 删除提现记录
     */
//    @PreAuthorize("@ss.hasPermi('pos:withdrawal:remove')" )
    @Log(title = "提现记录" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iBizWithdrawalRecordsService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }


    // app 提现记录
    @PostMapping("/app/back/records")
    public AjaxResult appBackRecords(@RequestBody BizWithdrawalRecords bizWithdrawalRecords){
        return iBizWithdrawalRecordsService.appBackRecords(bizWithdrawalRecords);
    }


    // 后台提现驳回
    @PostMapping("/refundWithdrawal")
    public AjaxResult refundWithdrawal(@RequestBody BizWithdrawalRecords bizWithdrawalRecords){
        return iBizWithdrawalRecordsService.refundWithdrawal(bizWithdrawalRecords);
    }

    // 后台统计
    @PostMapping("/executeGetStatistic")
    public AjaxResult executeGetStatistic(@RequestBody BizWithdrawalRecords bizWithdrawalRecords){
        return iBizWithdrawalRecordsService.executeGetStatistic(bizWithdrawalRecords);
    }



}
